package com.burst.reptile.modular.zp.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.exception.user.CaptchaException;
import com.burst.reptile.common.exception.user.CaptchaExpireException;
import com.burst.reptile.framework.redis.RedisCache;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.zp.entity.ZpHelpShop;
import com.burst.reptile.modular.zp.service.IZpHelpShopService;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 帮助找铺 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/zp/helpshop")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpHelpShopController extends BaseController{
	
	private final IZpHelpShopService helpShopService;
	
	private final RedisCache redisCache;

	/**
	 * 添加用户 帮助需求
	 * @param helpShop
	 * @return
	 */
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody ZpHelpShop helpShop){
		String uuid = helpShop.getUuid();
		String code = helpShop.getCode();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String captcha = redisCache.getCacheObject(verifyKey);
		redisCache.deleteObject(verifyKey);
		if (captcha == null) {
			throw new CaptchaExpireException();
		}
		if (!StrUtil.equalsIgnoreCase(code, captcha)) {
			throw new CaptchaException();
		}
		helpShopService.save(helpShop);
		return SUCCESS;
	}
	 
}
